//
//  Codable++.swift
//  iOS_Com_Extensions
//
//  Created by 家和20240415 on 2024/9/30.
//

import Foundation

public extension Encodable {
    ///  使用 JSONEncoder 将结构体转换为 Dictionary
    func toDictionary() -> [String:Any]? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted // 设置输出格式为可读格式
        do {
            let data = try encoder.encode(self)
            if let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
                debugPrint(dictionary)
                return dictionary
            }
        } catch {
            debugPrint(error)
        }
        
        return nil
    }
}
